<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Applcation
 *
 * @author eoe2005
 */
class Application {
    public function run($control,$action){
        $control = ucfirst($control).'Action';
        //$action = strtolower($action);
        $file = sprintf('%s/%s.php',APP_ACTION,$control);
        if(file_exists($file)){
            include $file;
            $cc = new $control;
            if(method_exists($cc, $action)){
               $rs = $cc->$action();
                if(is_integer($rs)){
                    Out::showError($rs);//这是出错了
                }
               Out::show($rs);//输出了
            }else{
                Debug::log($action.'方法不存在'.print_r($_GET,true));
                Out::showError(Error::INTEFACE_NOT_EXISTS);
            }
        }else{
            Debug::log($file.'文件不存在'.print_r($_GET,true));
            Out::showError(Error::INTEFACE_NOT_EXISTS);
        }
    }
    private static $_this;
    private function __construct(){
    }
    public static function Sigleton($appname){
        if(!self::$_this){
            $file = __ROOT.'config/app_'.$appname.'.php';
            if(function_exists('apache_getenv') AND apache_getenv('G_API_DEBUG')){
                $file = __ROOT.'config/app_'.$appname.'_jia.php';
            }
            if(file_exists($file)){
                include $file;//加载应用的配置文件
            }else{
                Debug::log('接口不存在'.print_r($_GET,true));
                Out::showError(Error::INTEFACE_NOT_EXISTS);
            }
            $c = __CLASS__;
            self::$_this = new $c;
        }
        return self::$_this;
    }
    public function __clone(){
        trigger_error('禁止创建多个实例');
    }
}
function getApp($appname){
    return Application::Sigleton($appname);
}
?>
